import { TourTransitionType } from "../enum/tourTransition.enum"
import transitionTypeEnum from "../enum/transitionType.enum"

export default class TourInstantTransition {
  moveToSweep: any
  viewmodeData: any
  cameraControl: any
  switchToMode: any
  toIndex: number
  type: number
  currentTransitionPromise: any
  onStopRequested: () => Promise<void>
  constructor(e, t, i, s) {
    ;(this.moveToSweep = e),
      (this.viewmodeData = t),
      (this.cameraControl = i),
      (this.switchToMode = s),
      (this.toIndex = -1),
      (this.type = TourTransitionType.Move),
      (this.currentTransitionPromise = null),
      (this.onStopRequested = () => Promise.resolve())
  }
  get active() {
    return null !== this.currentTransitionPromise
  }
  get promise() {
    return this.currentTransitionPromise ? this.currentTransitionPromise : Promise.resolve()
  }
  async stop() {
    this.currentTransitionPromise && (await this.onStopRequested(), await this.promise, (this.currentTransitionPromise = null))
  }
  start(e) {
    if (this.active) throw Error("Transition already active")
    if (!e.snapshot) return (this.currentTransitionPromise = Promise.resolve()), this
    const { deferred: t } = this.build(e.snapshot, e.currentSweep)
    return (
      (this.currentTransitionPromise = t.then(() => {
        this.currentTransitionPromise = null
      })),
      this
    )
  }
  build(e, t) {
    let i = Promise.resolve()
    const s = e.metadata.cameraMode,
      n = e.metadata.cameraQuaternion,
      r = e.metadata.scanId,
      o = {
        position: e.metadata.cameraPosition,
        rotation: n,
        sweepID: r,
        zoom: e.metadata.orthoZoom
      },
      a = s !== this.viewmodeData.currentMode,
      h = !!r && this.viewmodeData.isInside()
    if (a) i = this.switchToMode(s, transitionTypeEnum.Instant, o)
    else if (h) {
      const e = { transitionType: transitionTypeEnum.Instant, sweepId: r, rotation: n }
      i = this.moveToSweep(e).nativePromise()
    } else i = this.cameraControl.moveTo({ transitionType: transitionTypeEnum.Instant, pose: o }).nativePromise()
    return { deferred: i }
  }
}
